using System;
using System.Linq;
using System.Reflection;

namespace GeYiHome.Bussines.BusinesMapping.Adapters
{
    public class BaseModuleRegisterTypesMap
        : RegisterTypesMap
    {
        public BaseModuleRegisterTypesMap()
        {
            SetMaps();
        }

        private void SetMaps()
        {
            var asm = Assembly.GetExecutingAssembly();
            var mapperTypes = asm.GetTypes()
                .Where(x => x.Name.EndsWith("DomainToDto") || x.Name.EndsWith("DtoToDomain"));
            foreach (var mapperType in mapperTypes)
            {
                RegisterMap(Activator.CreateInstance(mapperType));
            }

            DefaulTypeMapConfigurationBase = (s, t) =>
            {
                var ty = typeof (Mapping<,>);
                ty = ty.MakeGenericType(s, t);
                dynamic ins = Activator.CreateInstance(ty);
                return ins;
            };
        }
    }
}